﻿@{ ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml"; }
@section head{

    <script type="text/javascript">
        var renderTable;
        var renderTableCRMUser;
        var userType = 0;

        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'form', 'treetable', 'index'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , treetable = layui.treetable
                , element = layui.element;
            element.render();
            element.on('tab(component-tabs-brief)', function (obj) {
                userType = obj.index;
                if (userType === 0) {
                    renderTable();
                } else {
                    renderTableCRMUser();
                }
            });
            // 渲染表格
            renderTable = function () {
                table.render({
                    elem: '#table-datalist'
                    , url: '/Console/Users/GetListJson'
                    , toolbar: '#table-datalist-toolbar'
                    , limit: 20
                    , title: '菜单数据表'
                    , cols: [[
                        { field: 'loginName', title: '登录名', width: 150 }
                        , { field: 'userName', title: '用户名', width: 150 }
                        , {
                            field: 'roles', title: '角色', templet: function (res) {
                                return '<span lay-event="' + res.roles + '"></span>';
                            }
                        }
                        , { field: 'remark', title: '备注' }
                        , { field: 'lastTime', title: '登录时间' }
                        , { field: 'lastIp', title: '登录IP' }
                        , {
                            field: 'state', title: '状态', width: 110, templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '">'
                            }
                        }
                        , { field: 'creator', title: '创建人', width: 150 }
                        , { field: 'operate', fixed: 'right', title: '操作', toolbar: '#table-datalist-bar', width: 220 }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');
                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //CRM用户列表
            renderTableCRMUser = function () {
                table.render({
                    elem: '#table-datalist-crmuser'
                    , url: '/Console/CRMAccount/GetListJson'
                    , toolbar: '#table-datalist-toolbar-crmuser'
                    , limit: 20
                    , title: '菜单数据表'
                    , cols: [[
                        { field: 'index', title: '序号', type: 'numbers', width: 60, templet: '#zizeng' }
                        , { field: 'loginName', title: '登录名', width: 150 }
                        , {
                            field: 'firstname', title: '姓名', width: 150, templet: function (res) {
                                return res.lastname + ' ' + res.firstname;
                            }
                        }
                        , {
                            field: 'state', title: '状态', width: 110, templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state-crmuser" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state-crmuser" value="' + res.id + '">'
                            }
                        }
                        , { field: 'telephone', title: '手机号' }
                        , { field: 'email', title: '邮箱' }
                        , { field: 'roles', title: '角色' }
                        , { field: 'createDate', title: '注册时间' }
                        , { field: 'operate', fixed: 'right', title: '操作', toolbar: '#table-datalist-bar-crmuser', width: 220 }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');
                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //加载表格
            if (userType === 0) {
                renderTable();
            } else {
                renderTableCRMUser();
            }

            /*管理员操作 开始 */
            //头工具栏事件
            table.on('toolbar(table-datalist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNewUser", 2, '800px', '650px', '/Console/Users/Create?level=0');
                        break;
                };
            });
            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var postData = {
                        id: data.id
                    };
                    top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/Users/Delete", "POST", "deleteSuccess", "deleteFail", function () {
                        renderTable();
                    });
                } else if (obj.event === 'edit') {
                    top.OpenPageByKey(layui, "editUser", 2, '800px', '600px', '/Console/Users/Edit?id=' + data.id + '&level=' + data.level);
                } else if (obj.event === 'add') {
                    top.OpenPageByKey(layui, "addNewUser", 2, '800px', '650px', '/Console/Users/Create?level=' + data.level + '&pid=' + data.id);
                } else if (obj.event === 'roles') {
                    top.OpenPageByKey(layui, "roles", 2, '700px', '600px', '/Console/Roles/Checkeds?userType=0&userid=' + data.id);
                }
            });

            //监听锁定操作
            form.on('switch(table-state)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/Users/UpdateState",
                    data: {
                        id: this.value,
                        intState: state,
                        userType: 0
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                        table.reload("table-datalist");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });
            /*管理员操作 结束 */


            /*CRM用户操作开始 */
            //头工具栏事件
            table.on('toolbar(table-datalist-crmuser)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNewUser", 2, '800px', '700px', '/Console/CRMAccount/Create?level=0');
                        break;
                };
            });
            //监听行工具事件
            table.on('tool(table-datalist-crmuser)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var postData = {
                        id: data.id
                    };
                    top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/CRMAccount/Delete", "POST", "deleteSuccess", "deleteFail", function () {
                        renderTableCRMUser();
                    });
                } else if (obj.event === 'edit') {
                    top.OpenPageByKey(layui, "editUser", 2, '800px', '600px', '/Console/CRMAccount/Edit?id=' + data.id);
                } else if (obj.event === 'add') {
                    top.OpenPageByKey(layui, "addNewUser", 2, '800px', '750px', '/Console/CRMAccount/Create');
                } else if (obj.event === 'roles') {
                    top.OpenPageByKey(layui, "roles", 2, '700px', '600px', '/Console/Roles/Checkeds?userType=1&userid=' + data.id);
                }
            });

            //监听锁定操作
            form.on('switch(table-state-crmuser)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/CRMAccount/UpdateState",
                    data: {
                        id: this.value,
                        intState: state,
                        userType: 1
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                        table.reload("table-datalist");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTableCRMUser();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });
            /*CRM用户操作结束 */
        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab layui-tab-brief" lay-filter="component-tabs-brief">
                        <ul class="layui-tab-title">
                            <li lay-event="manageMenus" class="layui-this">管理员</li>
                            <li lay-event="CRMusers">用户</li>
                        </ul>
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show">
                                <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                                <script type="text/html" id="table-datalist-toolbar">
                                    <div class="layui-btn-container">
                                        <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                                    </div>
                                </script>
                                <script type="text/html" id="table-datalist-bar">
                                    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</button>
                                    {{#if(d.id!=1){}}
                                    <button class="layui-btn layui-btn-xs layui-btn-warm" lay-event="roles">角色</button>
                                    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
                                    {{#}}}
                                </script>
                            </div>
                            <div class="layui-tab-item">
                                <table class="layui-hide" id="table-datalist-crmuser" lay-filter="table-datalist-crmuser"></table>
                                <script type="text/html" id="table-datalist-toolbar-crmuser">
                                    <div class="layui-btn-container">
                                        <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                                    </div>
                                </script>
                                <script type="text/html" id="table-datalist-bar-crmuser">
                                    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</button>
                                    <button class="layui-btn layui-btn-xs layui-btn-warm" lay-event="roles">角色</button>
                                    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>